package cate.game.role.pay.trademall.vipprivilege;

import cate.common.table.d.GDOperation;
import cate.common.table.role.vip.RoleVipRow;
import cate.game.res.MixRes;
import cate.game.res.MixResItem;
import cate.game.role.Role;
import cate.game.role.RoleDependent;
import cate.game.role.pay.trademall.vipprivilege.msg.VipPrivilegeResp;
import com.fasterxml.jackson.annotation.JsonIgnore;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;
import easy.java.practice.errorcode.EcResult;

import java.util.HashMap;
import java.util.Map;

@NoteClass("vip特权数据")
public class VipPrivilege extends RoleDependent {
    @NoteField(value = "特权礼包购买情况", detail = "<vip等级，是否购买>")
    public Map<Integer, Boolean> giftMap;

    @NoteField(value = "是否领取过福利")
    public Map<Integer, Boolean> rewardGots;

    @Override
    public void initialize(Role role) {
        super.initialize(role);
        if (giftMap == null) {
            giftMap = new HashMap<>();
        }
        if (rewardGots == null) {
            rewardGots = new HashMap<>();
        }
    }

    public EcResult<Void> buyVipGift(int vip) {
        EcResult<Void> r = new EcResult<>();
        if (vip > role.getBase().vip) {
            return r.fail("未达到VIP" + vip);
        }
        if (giftMap.get(vip) != null) {
            return r.fail("已售完");
        }
        RoleVipRow row = getRow(vip);
        if (row == null) {
            return r.fail("礼包不存在！");
        }
        EcResult<MixResItem> consumeRes = new MixRes(row.giftConsumeStr).consume(role, null);
        if (!consumeRes.ok()) {
            role.getGame().notice.tipResItem(role, consumeRes.data);
            //为了不弹字
            return r.success();
        }
        MixRes reward = new MixRes(row.giftStr);
        reward.add(role, true, GDOperation.VIP_GIFT_BUY);
        role.getGame().notice.dialogReward(role, reward);
        giftMap.put(vip, true);
        noticeUpdate();
        return r;
    }

    @JsonIgnore
    private RoleVipRow getRow(int vip) {
        return role.getGame().table.vip.base.getByLevel(vip);
    }

    @JsonIgnore
    public EcResult<Void> getReward(int vip) {
        EcResult<Void> r = new EcResult<>();
        RoleVipRow row = getRow(vip);
        if (row == null) {
            return r.fail("福利不存在！");
        }
        if (vip > role.getBase().vip) {
            return r.fail("不可领取该档位的福利！");
        }
        if (rewardGots.containsKey(vip) && rewardGots.get(vip).equals(Boolean.TRUE)) {
            return r.fail("已领取过此福利！");
        }
        MixRes reward = new MixRes(row.rewardStr);
        reward.add(role, true, GDOperation.VIP_DAILY_REWARD);
        role.getGame().notice.dialogReward(role, reward);
        rewardGots.put(vip, true);
        role.getHistory().action.receiveVipReward();
        noticeUpdate();
        return r;
    }

    @Override
    public void onDaySpan(boolean silence) {
    }

    public void noticeUpdate() {
        role.sendNow(new VipPrivilegeResp(this));
    }
}
